package com.easy.easy.util;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.AlipayConfig;
import com.alipay.api.diagnosis.DiagnosisUtils;
import com.alipay.api.domain.AlipayTradePrecreateModel;
import com.alipay.api.domain.ExtendParams;
import com.alipay.api.response.AlipayTradePrecreateResponse;
import com.alipay.api.domain.GoodsDetail;
import com.alipay.api.domain.BusinessParams;
import com.alipay.api.request.AlipayTradePrecreateRequest;

import com.alipay.api.FileItem;
import com.easy.easy.bean.Order;

import java.util.Base64;
import java.util.ArrayList;
import java.util.List;

public class PaymentUtil {
    //public static void main(String[] args) throws AlipayApiException {



    public static String payment(Order order) throws AlipayApiException {
        // 初始化SDK
        AlipayClient alipayClient = new DefaultAlipayClient(getAlipayConfig());

        // 构造请求参数以调用接口
        AlipayTradePrecreateRequest request = new AlipayTradePrecreateRequest();
        AlipayTradePrecreateModel model = new AlipayTradePrecreateModel();




        // 设置商户订单号
        /*model.setOutTradeNo("20150320010101001");*/
        model.setOutTradeNo(String.valueOf(order.getCode()));

        // 设置订单总金额
        model.setTotalAmount(order.getMoney().toString());//

        // 设置订单标题
        model.setSubject("百货商城");

        // 设置产品码
        model.setProductCode("FACE_TO_FACE_PAYMENT");

        // 设置卖家支付宝用户ID
        model.setSellerId("2088721061036105");

        // 设置订单附加信息
        /*model.setBody("Iphone6 16G");*/

        // 设置订单包含的商品列表信息
//        List<GoodsDetail> goodsDetail = new ArrayList<GoodsDetail>();
//        GoodsDetail goodsDetail0 = new GoodsDetail();
//        goodsDetail0.setGoodsName("ipad");
//        goodsDetail0.setQuantity(1L);
//        goodsDetail0.setPrice("2000");
//        goodsDetail0.setGoodsId("apple-01");
//        goodsDetail0.setGoodsCategory("34543238");
//        goodsDetail0.setCategoriesTree("124868003|126232002|126252004");
//        goodsDetail0.setShowUrl("http://www.alipay.com/xxx.jpg");
//        goodsDetail.add(goodsDetail0);
//        model.setGoodsDetail(goodsDetail);
        // 设置业务扩展参数
       /* ExtendParams extendParams = new ExtendParams();
        extendParams.setSysServiceProviderId("2088511833207846");
        model.setExtendParams(extendParams);*/

        // 设置商户传入业务信息
//        BusinessParams businessParams = new BusinessParams();
//        businessParams.setMcCreateTradeIp("127.0.0.1");
//        model.setBusinessParams(businessParams);

        // 设置可打折金额
//        model.setDiscountableAmount("80.00");

        // 设置商户门店编号
//        model.setStoreId("NJ_001");

        // 设置商户操作员编号
//        model.setOperatorId("yx_001");

        // 设置商户机具终端编号
//        model.setTerminalId("NJ_T_001");

        // 设置商户的原始订单号
//        model.setMerchantOrderNo("20161008001");

        request.setBizModel(model);
        request.setNotifyUrl("http://3b52b6f8.r37.cpolar.top/order/paymentsuccess");
        // 第三方代调用模式下请设置app_auth_token
        // request.putOtherTextParam("app_auth_token", "<-- 请填写应用授权令牌 -->");

        AlipayTradePrecreateResponse response = alipayClient.execute(request);
        System.out.println(response.getBody());

        if (response.isSuccess()) {
            String body=response.getBody();
            JSONObject obj=JSON.parseObject(body);
            obj=obj.getJSONObject("alipay_trade_precreate_response");
            String qr_code=obj.getString("qr_code");
            System.out.println(qr_code);
            System.out.println("调用成功");
            return qr_code;
        } else {
            System.out.println("调用失败");
            String diagnosisUrl= DiagnosisUtils.getDiagnosisUrl(response);
            System.out.println(diagnosisUrl);
            return null;
            // sdk版本是"4.38.0.ALL"及以上,可以参考下面的示例获取诊断链接
            // String diagnosisUrl = DiagnosisUtils.getDiagnosisUrl(response);
            // System.out.println(diagnosisUrl);
        }
    }

    private static AlipayConfig getAlipayConfig() {
        String privateKey  = "MIIEvAIBADANBgkqhkiG9w0BAQEFAASCBKYwggSiAgEAAoIBAQCScn4ljFwgviPyroaHeuAJkXG45jZ8u3CaDbuFl3pgQ14YZavYi0ul65pkdstMs/evyEX3xlD6NfTgEpgtZS3W1MOLiec/w+hnREfemPGzj8OXJHDB8uzFLLiuoGX6NdFAijnH903Mpg+6OaO79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AdAg1nEm1aHkdMguRYKpiRt/dbC70AO1GXqgk/xFzLatN8oZ4pD6jN/1gkGIVUBtdaQTDLR9rJmJFg7BFRmL8160+Pitc/amWL2igaHjTC2OWGDjwgjEy6K5o4Eex/+YDgGNYytpNJfZFoYN4lqC6g+L7hBQ0ev8tU2K14jQjF/cCgYBIgEaz1LVOvXm0q431Yny7WIwCJN+VzGBteSAvUFhBIYuVAJCrUlOWuarZ+6ANdW3qT2qcH9A6zHpjqi8CZu5bAN6OMvA4blqVWaiJUn/BnMeCHnU62nr0+//gesT8++fCLRenFTgia0lZHFYyv9WEM/VIwCJU6nSgCVDdybcjbQ==";
        String alipayPublicKey = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAl1748fuplz1VLw1YIM6zpypeuAXj5iFIRyjn0Ii/zT2WJAulcbedW7dYo1THulh8Ilp29kj3cNMhpmCdeaLEajtzfSeThSiHpRWtMnT+d3332WRL2Hg9Ovs436l/4k5gk3CMnL6YBskpbToZHGu4Ff77nWmA2djwaDaAYBMoCxqFEj7KNU9SyhvBlwh+NEcYIG3hy6+LtHi+mn+x9e+iKqwO1P6+xg6phZUkk2D9Pv0Vx/jWBJAw+nTBcllSxP4SMvCBas2nYhiAb+/zDQ9f19wuJYdfTT+AbNTFytok+AT2z5u9cgG28aqBTSGjtI6utoe7YFPCh1uuTG6jhhV7+wIDAQAB";
        AlipayConfig alipayConfig = new AlipayConfig();
        alipayConfig.setServerUrl("https://openapi-sandbox.dl.alipaydev.com/gateway.do");
        alipayConfig.setAppId("9021000146601444");
        alipayConfig.setPrivateKey(privateKey);
        alipayConfig.setFormat("json");
        alipayConfig.setAlipayPublicKey(alipayPublicKey);
        alipayConfig.setCharset("UTF-8");
        alipayConfig.setSignType("RSA2");
        return alipayConfig;
    }
}